package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    // 提交订单
    @PostMapping("submit")
    public R submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }
    //移动端分页查询
    @GetMapping("userPage")
    public R userPage(Integer page, Integer pageSize) {
        Page<OrdersDto> list= ordersService.pageWithDetail(page,pageSize);
        return R.success(list);
    }
    //后端  ge >= 和 le<=
    @GetMapping("page")
    public R Page(Integer page, Integer pageSize, Long number,
                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<Orders> page1=new Page(page,pageSize);
        ordersService.lambdaQuery()
                .eq(number!=null, Orders::getId,number)
                .between(beginTime!=null && endTime!=null,Orders::getOrderTime,beginTime,endTime)
                .page(page1);
        return R.success(page1);
    }
    //修改状态
    @PutMapping()
    public R update(@RequestBody Orders orders){
        ordersService.lambdaUpdate()
                .eq(Orders::getId,orders.getId())
                .set(Orders::getStatus,orders.getStatus())
                .update();
        return R.success("修改成功");
    }
}
